#include "FiveWin.ch"

#define CB_ADDSTRING     ( WM_USER +  3 )
#define CB_GETCURSEL     ( WM_USER +  7 )
#define CB_SETCURSEL     ( WM_USER + 14 )
#define CB_ERR                       -1
#define CBS_DROPDOWNLIST              3

#define LBS_NOTIFY                    1   // 0x0001L
#define LBS_DISABLENOSCROLL        4096   // 0x1000L

//----------------------------------------------------------------------------//

CLASS TComboBox FROM TControl

   DATA   aItems
   DATA   bChange, bSetGet

   METHOD New( nRow, nCol, bSetGet, aItems, nWidth, nHeight, oWnd, nHelpId,;
               bChange ) CONSTRUCTOR
   METHOD ReDefine( nId, bSetGet, aItems, oWnd, nHelpId, bValid, bChange ) ;
                                                                 CONSTRUCTOR
   METHOD cToChar() BLOCK { | Self | Super:cToChar( "COMBOBOX" ) }
   METHOD Init( hDlg )
   METHOD Change()
   METHOD Refresh()
   METHOD Select( nItem ) BLOCK ;
          { | Self, nItem | ::SendMsg( CB_SETCURSEL, nItem, 0 ) }
   METHOD SetFocus( lOnOff )

ENDCLASS

//----------------------------------------------------------------------------//

METHOD New( nRow, nCol, bSetGet, aItems, nWidth, nHeight, oWnd, nHelpId,;
            bChange ) CLASS TComboBox

   DEFAULT aItems := {}, nWidth := 40, nHeight := 60

   ::cCaption = ""
   ::nTop     = nRow * 14
   ::nLeft    = nCol *  8
   ::nBottom  = ::nTop  + nHeight - 1
   ::nRight   = ::nLeft + nWidth  - 1
   ::aItems   = aItems
   ::bChange  = bChange
   ::bSetGet  = bSetGet
   ::oWnd     = oWnd
   ::nStyle   = nOR( LBS_NOTIFY, WS_TABSTOP, CBS_DROPDOWNLIST,;
                     LBS_DISABLENOSCROLL, WS_CHILD, WS_VISIBLE, WS_BORDER,;
                     WS_VSCROLL )
   ::nId      = ::GetNewId()
   ::nHelpId  = nHelpId

   if oWnd:ClassName() != "TDIALOG"
      ::Create( "COMBOBOX" )
   else
      oWnd:DefControl( Self )
   endif

return

//----------------------------------------------------------------------------//

METHOD ReDefine( nId, bSetGet, aItems, oWnd, nHelpId, bValid, bChange ) ;
                                                               CLASS TComboBox
   DEFAULT aItems := {}

   ::nId     = nId
   ::hWnd    = 0
   ::aItems  = aItems
   ::bChange = bChange
   ::bSetGet = bSetGet
   ::oWnd    = oWnd
   ::nHelpId = nHelpId
   ::bValid  = bValid

   oWnd:DefControl( Self )

return

//----------------------------------------------------------------------------//

METHOD Init( hDlg ) CLASS TComboBox

   ::hWnd = GetDlgItem( hDlg, ::nId )
   AEval( ::aItems, { | cItem | ::SendMsg( CB_ADDSTRING, 0, cItem ) } )
   ::Select( 0 )
   ::Link()

return

//----------------------------------------------------------------------------//

METHOD Change() CLASS TComboBox

   ::Refresh()

   if ::bChange != nil
      Eval( ::bChange, Self )
   endif

return

//----------------------------------------------------------------------------//

METHOD Refresh() CLASS TComboBox

   local nAt := ::SendMsg( CB_GETCURSEL )

   if nAt != CB_ERR
      Eval( ::bSetGet, ::aItems[ nAt + 1 ] )
   endif

   Super:Refresh()

return

//----------------------------------------------------------------------------//

METHOD SetFocus( lOnOff ) CLASS TComboBox

   local nAt

   if ! lOnOff
      nAt = ::SendMsg( CB_GETCURSEL )
      if nAt != CB_ERR
         Eval( ::bSetGet, ::aItems[ nAt + 1 ] )
      endif
   endif

return

//----------------------------------------------------------------------------//
